﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FirmaTransportowa
{
    public partial class DriverPanel : Form
    {
        SqlConnection dbConnection;
        int userID;
        static DriverBeginTransport driverBeginTransport;
        static DriverFinishTransport driverFinishTransport;
        static DriverTransportsList driverTransportsList;

        public DriverPanel(int id)
        {
            SqlConnector connection = new SqlConnector();
            dbConnection = connection.getInstance();
            try
            {
                dbConnection.Open();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            InitializeComponent();
            DateTime dateTime = DateTime.Now;
            driverDateAndTime.Text = dateTime.ToString();
            userID = id;
            SqlCommand selectName = new SqlCommand("SELECT w.name, w.surname FROM Workers w WHERE w.worker_ID = (SELECT u.worker_id FROM Users u WHERE u.user_ID = " + userID + ")", dbConnection);
            SqlDataReader nameReader = null;
            nameReader = selectName.ExecuteReader();
            nameReader.Read();
            driverName.Text = nameReader["name"].ToString() + " " + nameReader["surname"].ToString();
            driverBeginTransport = new DriverBeginTransport(userID);
            driverFinishTransport = new DriverFinishTransport(userID);
            driverTransportsList = new DriverTransportsList(userID);
            nameReader.Close();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                Application.Exit();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            driverDateAndTime.Text = DateTime.Now.ToString();
        }

        private void beginTransport_Click(object sender, EventArgs e)
        {
            SqlCommand command = new SqlCommand("SELECT * FROM Transport WHERE status = 'w trakcie' AND driver_ID = (SELECT worker_ID FROM Users WHERE user_ID = " + userID + ")", dbConnection);
            SqlDataReader reader = null;
            reader = command.ExecuteReader();
            if (reader.Read())
            {
                reader.Close();
                MessageBox.Show("Posiadasz rozpoczęty transport!");
            }
            else
            {
                reader.Close();
                SqlCommand checkDriverTransports = new SqlCommand("SELECT * FROM Transport WHERE status = 'zaplanowany' AND driver_ID = (SELECT worker_ID FROM Users WHERE user_ID = " + userID + ")", dbConnection);
                SqlDataReader check = null;
                check = checkDriverTransports.ExecuteReader();
                if (check.Read())
                {
                    if (driverBeginTransport.IsDisposed) driverBeginTransport = new DriverBeginTransport(userID);
                    driverBeginTransport.Show();
                }
                else
                {
                    MessageBox.Show("Nie posiadasz żadnych zaplanowanych transportów");
                }
                check.Close();
            }
        }

        private void finishTransport_Click(object sender, EventArgs e)
        {
            SqlCommand command = new SqlCommand("SELECT * FROM Transport WHERE status = 'w trakcie' AND driver_ID = (SELECT worker_ID FROM Users WHERE user_ID = " + userID + ")", dbConnection);
            SqlDataReader reader = null;
            reader = command.ExecuteReader();
            if (!reader.Read())
            {
                MessageBox.Show("Nie posiadasz rozpoczętego transportu!");
            }
            else 
            {
                if (driverFinishTransport.IsDisposed) driverFinishTransport = new DriverFinishTransport(userID);
                driverFinishTransport.Show();
            }
            reader.Close();
        }

        private void myTransports_Click(object sender, EventArgs e)
        {
            if (driverTransportsList.IsDisposed) driverTransportsList = new DriverTransportsList(userID);
            driverTransportsList.Show();
        }
    }
}